# coding: utf-8
from qeventmanager import qevent_manager

from common.server import BananaServer
from components import GridScrollArea, VideoCard
from ..tools import check_result


class MinVideoInterface(GridScrollArea):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setObjectName("MinVideoInterface")

    def init(self):
        if not self.isLoad:
            self.load()
            self.isLoad = True

    def load(self):
        qevent_manager.addTask(BananaServer.minivod, self.currentPage, slot=self.on_handle_minivod_data)

    def on_handle_minivod_data(self, result: dict):
        data = check_result(result)
        if not data:
            return
        for index, row in enumerate(data['rows']):
            vodrow = row.get('vodrow')
            if vodrow.get('isvip') != '0':
                continue
            card = VideoCard(self)
            card.setMin(True)
            card.setData(vodrow)
            card.finished.connect(lambda obj: obj.scaledToWidth(300))
            self.addWidget(card)
        if self.count() <= 20:
            self.nextPage()

    def nextPage(self):
        super().nextPage()
        self.load()
